<?php
/* @var $data array of ECalendarViewItem */
/* @var $id int pk of Cliente*/

?>

<div class="view" >
 
<span style="line-height:35px;font-size:23px;font-weight:bold;"> <?php echo $data->date->format('j'); ?> </span>
<br>

<?php 

if($data->date->format('D')!='Sun'):
	$citasDelDia= Cita::model()->getCitasIntervalo($data->date->format('Y-m-d'), $data->date->format('Y-m-d'));
	$horas = array('08:30',
				   '09:00',
				   '09:30',
				   '10:00',
				   '10:30',
				   '11:00',
				   '11:30',
				   '12:00',
				   '12:30',
				   '13:00',
				   '13:30',
				   '14:00',
				   '14:30',
				   '15:00',
				   '15:30',
				   '16:00',
				   '16:30',
				   '17:00',
				   '17:30',
				   '18:00',
				   );			   
	
	$doctor=0;

	//will hold max hours depending if weekday or saturday
	if($data->date->format('D')=='Sat'){
		$startHorasPos = 0;
		$maxHorasPos = 12;				
	} else {
		$startHorasPos = 1;
		$maxHorasPos = 20;				
	
	}
	
	for ($i=$startHorasPos; $i<$maxHorasPos; $i++):

		$doctor=0;
		
		//keeps Cita models that match the current time
		$citasHoraUsadas = array();
		
		/*compare the current hour against the hora inside each day
		  to check for matching citas already scheduled
		*/
		for ($j=0; $j < sizeof($citasDelDia); $j++){
			if (strncmp($citasDelDia[$j]->hora,$horas[$i],5) == 0){
				$citasHoraUsadas[]= $citasDelDia[$j];
			}			
		}
		
		/*check if there are 3 or more Citas on this hora already
		  if there are then it will print that this hour and 
		  the next half-hour are full
		*/
		if(sizeof($citasHoraUsadas)>=3):
		?>
		
		<span style="color:red;font-size:12px"><?php echo $horas[$i].'-'.$horas[$i+1];?> (Agotadas) </span>
		<br>
		
		<?php
		$i++;
		/*checks if hora has citas in it,
		  if it doesn't then just creates a link
		  choosing the client's favorite doctor in it
		*/
		elseif(sizeof($citasHoraUsadas)<1): 
		?>
		
			<div class="citaDayDisponible">
				<?php 
				echo CHtml::link(
				$horas[$i], 
				array('create', 'id'=>$id,
								'doctor'=>Cliente::model()->findByPk($id)->idDoctor,
								'date'=>$data->date->format('Y-m-d'), 
								'time'=>$horas[$i],
								),
				array('class'=>'citaDayDisponible')
				);
				?>
				<br>
				
			</div>
		
		<?php 
		else:
			$cliente = Cliente::model()->findByPk($id);
			$doctorFav=$cliente->idDoctor;
				
			for ($k=0; $k < sizeof($citasHoraUsadas); $k++){
				if($doctorFav==$citasHoraUsadas[$k]->idDoctor)
					break 1;
				else
					$doctor=$doctorFav;
			}
			
			if($doctor!=$doctorFav)
				$doctor= Doctor::getRandDocId($doctorFav);
		?>
			<div class="citaDayDisponible">
				<?php 
				echo CHtml::link(
				$horas[$i], 
				array('create', 'id'=>$id,
								'doctor'=>$doctor,
								'date'=>$data->date->format('Y-m-d'), 
								'time'=>$horas[$i],
								),
				array('class'=>'citaDayDisponible')
				);
				?>
				<br>
				
			</div>
	<?php
		endif;	
	endfor;
else:?>

	<div>
		<hr>
		<br>
		<span style="font-size: 80%; color:red;">Cerrado</span>
		<br>
		<br>
		<hr>
	</div>
<?php 
endif;	?>


</div>
